package com.helloandroid.dialogs

import android.app.Activity
import android.content.Context
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.business.Analyse
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.ads.NativeAdUtil
import com.helloandroid.ads.VideoAdUtil
import com.helloandroid.base.BaseDialog
import com.helloandroid.tools.*

class AwardDialog(activity: Context, val coinCount: Int, val isDoubleGet: Boolean = true) : BaseDialog(activity) {

    // 用于统计 区分 领取的是哪一个双倍
    var reasonKey: String? = null

    private lateinit var tvMsg: TextView
    private lateinit var tvMyCoin: TextView
    private lateinit var btnDouble: Button
    lateinit var llCoinInfo: LinearLayout
    lateinit var tvDrop: TextView
    lateinit var ivBtnClose: ImageView

    //
    var fakeCoin = 0

    constructor(fragment: Fragment, coinCount: Int) : this(fragment.requireActivity(), coinCount) {}

    override fun getContentViewResId() = R.layout.dialog_award

    override fun initView() {
        val user = AppUtil.user

        llCoinInfo = findViewById(R.id.llCoinInfo)
        tvMsg = findViewById(R.id.tvMsg)
        tvMyCoin = findViewById(R.id.tvMyCoin)
        btnDouble = findViewById(R.id.btnDouble)
        tvDrop = findViewById(R.id.tvDrop)
        ivBtnClose = findViewById(R.id.ivBtnClose)

        // 开始隐藏
        ivBtnClose.visibility = View.INVISIBLE

        // 2s后显示出放弃按钮
        tvDrop.alpha = 0f
        tvDrop.postDelayed({
            tvDrop.alpha = 1f
            tvDrop.setOnClickListener { cancel() }
        }, AppUtil.serverConfig.giveupDoubleRewardDelay.toLong())

        // 取消掉 动画
//        val valueAnimator = ValueAnimator.ofFloat(0f,1f)
//        MyLog.info("AppUtil.serverConfig.giveupDoubleRewardDelay:${AppUtil.serverConfig.giveupDoubleRewardDelay}")
//        valueAnimator.duration = AppUtil.serverConfig.giveupDoubleRewardDelay.toLong()
//        valueAnimator.addUpdateListener {va->
//            tvDrop.alpha = va.animatedValue as Float
//            if (va.animatedFraction >= 1.0f){
//                MyLog.info("动画结束!")
//                tvDrop.setOnClickListener { cancel() }
//            }
//        }
//        valueAnimator.start()

        tvMsg.text = "恭喜获得${coinCount}金币"
        tvMyCoin.text = "${user.coin} ≈ ${GUtils.coinToPrecision2Money(user.coin)}元"
        if (AppConfig.isFake) {
            llCoinInfo.visibility = View.INVISIBLE
        } else {
            llCoinInfo.visibility = View.VISIBLE
        }

        fakeCoin = user.coin

        findViewById<ImageView>(R.id.ivBtnClose).setOnClickListener { cancel() }

        if (isDoubleGet) {
            // 包含播放按钮
            btnDouble.text = "双倍领取"
        } else {
            btnDouble.text = "关闭"
        }

        btnDouble.setOnClickListener(object : ThrottleClickListener() {
            override fun noDoubleClick(v: View?) {
                if (isDoubleGet) {
                    playVideo()
                } else {
                    cancel()
                }
            }
        })

        val activity = ctx as Activity
        NativeAdUtil.showNativeAd(activity, findViewById(R.id.nativeView))
    }

    override fun initData() {}

    private fun playVideo() {
        if (!NetWorkUtil.networkConnected()) {
            AppUtil.toast("请打开网络!")
            return
        }
        VideoAdUtil.playVideoAd(ctx as Activity) { success ->
            if (success) {

                reasonKey?.let {
                    Analyse.report(it)
                }

                MyLog.elog("播放视频成功,给与奖励!")
                AppUtil.toast("恭喜领取${coinCount}金币!")
                val appModel = AppUtil.appViewModel()
                appModel.doubleAward(coinCount)
                btnDouble.visibility = View.GONE

                tvDrop.visibility = View.INVISIBLE
                ivBtnClose.visibility = View.VISIBLE

                // 服务端可能返回 也可能还没返回
                fakeCoin += coinCount
                tvMyCoin.text = "${fakeCoin} ≈ ${GUtils.coinToPrecision2Money(fakeCoin)}元"
            } else {
                AppUtil.toast("视频还没准备好!")
            }
        }
    }
}


